/*
	HTML Plugin
*/
HTMLEdManager.TablePlugin = {};
HTMLEdManager.TablePlugin._cur_doc = null;
HTMLEdManager.TablePlugin._cur_editor = null;

HTMLEdManager.TablePlugin.create_table = function ( doc, editor )
{
	//HTMLEdManager.TablePlugin.create_table._cur_doc = doc;
	HTMLEdManager.TablePlugin._cur_doc = doc;
	HTMLEdManager.TablePlugin._cur_editor = editor;

	liwe.lightbox.fade = false;
	liwe.lightbox.easy ( "htmled_table", "Inserimento tabella", 500, 160 );

	$ ( "htmled_table" ).innerHTML = '<table border="0" width="100%">' +
				'<tr>' +
				'	<td>Righe:</td>' +
				'	<td><input type="text" id="htmled_table_rows" value="3" /></td>' +
				'	<td>Colonne:</td>' +
				'	<td><input type="text" id="htmled_table_cols" value="2" /></td>' +
				'</tr>' +
				'<tr>' +
				'	<td>Larghezza:</td>' +
				'	<td><input type="text" id="htmled_table_width" value="200" /></td>' +
				'	<td>Altezza:</td>' +
				'	<td><input type="text" id="htmled_table_height" value="" /></td>' +
				'</tr>' +
				'<tr>' +
				'	<td>Spaziatura celle:</td>' +
				'	<td><input type="text" id="htmled_table_cellsp" value="1" /></td>' +
				'	<td>Padding celle:</td>' +
				'	<td><input type="text" id="htmled_table_cellpad" value="1" /></td>' +
				'</tr>' +
				'<tr>' +
				'	<td>Bordo:</td>' +
				'	<td><input type="text" id="htmled_table_border" value="1" /></td>' +
				'	<td>Allineamento:</td>' +
				'	<td>' +
				'		<select id="htmled_table_align">' +
				'			<option value="left" selected="selected">Sinistra</option>' +
				'			<option value="center">Centrato</option>' +
				'			<option value="right">Destra</option>' +
				'		</select>' +
				'	</td>' +
				'</tr>' +
				'<tr>' +
				'	<td align="center" colspan="4">' +
				'		<button type="button" onclick="HTMLEdManager.TablePlugin.create_table_done()">Ok</button>' +
				'		<button type="button" onclick="liwe.lightbox.close()">Annulla</button>' +
				'	</td>' +
				'</tr>' +
				'</table>';
};


HTMLEdManager.TablePlugin.create_table_done = function ()
{
	var rows = $v ( "htmled_table_rows" );
	var cols = $v ( "htmled_table_cols" );
	var width = $v ( "htmled_table_width" );
	var height = $v ( "htmled_table_height" );
	var cellsp = $v ( "htmled_table_cellsp" );
	var cellpad = $v ( "htmled_table_cellpad" );
	var border = $v ( "htmled_table_border" );
	var align = $v ( "htmled_table_align" );

	liwe.lightbox.close ();

	//var doc = HTMLEdManager.TablePlugin.create_table._cur_doc;
	var doc = HTMLEdManager.TablePlugin._cur_doc;
	var editor = HTMLEdManager.TablePlugin._cur_editor;
	var sel = HTMLEdManager.getSelection ( doc, editor );

	var t = doc.createElement ( "TABLE" );
	
	t.setAttribute ( "border", border );
	t.setAttribute ( "width", width );
	t.setAttribute ( "height", height );
	t.setAttribute ( "align", align );
	t.setAttribute ( "cellPadding", cellpad );
	t.setAttribute ( "cellSpacing", cellsp );

	if ( ! rows ) rows = 0;
	else rows = parseInt ( rows, 10 );

	if ( ! cols ) cols = 0;
	else cols = parseInt ( cols, 10 );

	var tbody = doc.createElement ( "TBODY" );
	t.appendChild ( tbody );

	var r;
	for ( r = 0; r < rows; r ++ )
	{
		var tr = doc.createElement ( "TR" );
		tbody.appendChild ( tr );
		
		var c;
		for ( c = 0; c < cols; c ++ )
		{
			var td = doc.createElement ( "TD" );
			tr.appendChild ( td );

			if ( navigator.userAgent.indexOf ( "Gecko" ) >= 0 )
			{
				var br = doc.createElement ( "BR" );
				br.setAttribute ( "type", "_moz" );
				td.appendChild ( br );
			}
		}
	}

	console.debug ( t );
	//doc.body.appendChild ( t );
	sel.insertNode ( t );
};


HTMLEdManager.register_plugin ( "table", 
{
	actions : [
			[ "create_table", HTMLEdManager.TablePlugin.create_table, "create_table" ]
	]
} );

